home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1810 / 1810.xpi / chrome / showcase.jar / content / settings / prefs.js < prev    next >
Text File  |  2010-01-17  |  7KB  |  190 lines

  1. var showcase_gPrefBranch = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.showcase.");
  2.  
  3. function showcase_initPrefs() {
  4.     if(document.documentElement.getButton && document.documentElement.getButton('extra2')) {
  5.         var extra2 = document.documentElement.getButton('extra2');
  6.     extra2.setAttribute('type', 'menu');
  7.         extra2.setAttribute('popup', 'showcase-settings-popup');
  8.     }
  9. }
  10.  
  11. function showcase_defaultSettings() {
  12.     var strings = document.getElementById("prefsStrings");
  13.     if(!confirm(strings.getString('prefs.confirm'))) return false;
  14.  
  15.     var count = { value : 0 };
  16.  
  17.       var childList = showcase_gPrefBranch.getChildList("", count);
  18.  
  19.     for(var i = 0; i < count.value; i++) {
  20.         if(showcase_gPrefBranch.prefHasUserValue(childList[i]) && childList[i] != "changelog") {
  21.             showcase_gPrefBranch.clearUserPref(childList[i]);
  22.         }
  23.     }
  24.  
  25.       window.alert(strings.getString('prefs.defaultsuccess'));
  26.     return true;
  27. }
  28.  
  29. function showcase_extendInt(aInput) {
  30.     if(aInput < 10) return "0" + aInput.toString();
  31.     else return aInput;
  32. }
  33.  
  34. function showcase_exportSettings() {
  35.     var now = new Date();
  36.     var sDate = showcase_extendInt(now.getMonth() + 1) + "/" + showcase_extendInt(now.getDate()) + "/" + now.getFullYear();
  37.     var sTtime = showcase_extendInt(now.getHours()) + ":" + showcase_extendInt(now.getMinutes()) + ":" + showcase_extendInt(now.getSeconds());
  38.     var sGMT = now.toGMTString();
  39.  
  40.     var showcaseExport = new Array;
  41.     showcaseExport[0] = "-----------------------------------------------------------------------\n";
  42.     showcaseExport[0]+= "                         Showcase - Settings\n";
  43.     showcaseExport[0]+= "          " + sDate + ", " + sTtime + " (" + sGMT + ")\n";
  44.     showcaseExport[0]+= "-----------------------------------------------------------------------\n";
  45.     showcaseExport[0]+= "                         DO NOT EDIT THIS FILE\n";
  46.     showcaseExport[0]+= "-----------------------------------------------------------------------";
  47.  
  48.     var count = { value : 0 };
  49.     var childList = showcase_gPrefBranch.getChildList("", count);
  50.  
  51.     for(var i = 0; i < count.value; i++) {
  52.         try {
  53.             switch(showcase_gPrefBranch.getPrefType(childList[i])) {
  54.                 case     Components.interfaces.nsIPrefBranch.PREF_BOOL:
  55.                             showcaseExport[i+1] = childList[i] + '=' + showcase_gPrefBranch.getBoolPref(childList[i]);
  56.                             break;
  57.  
  58.                 case     Components.interfaces.nsIPrefBranch.PREF_INT:
  59.                             showcaseExport[i+1] = childList[i] + '=' + showcase_gPrefBranch.getIntPref(childList[i]);
  60.                             break;
  61.  
  62.                 case     Components.interfaces.nsIPrefBranch.PREF_STRING:
  63.                             showcaseExport[i+1] = childList[i] + '=' + showcase_gPrefBranch.getCharPref(childList[i]);
  64.                             break;
  65.             }
  66.         }
  67.         catch(e) { }
  68.     }
  69.  
  70.     showcaseExport.sort();
  71.     showcase_saveToFile(showcaseExport);
  72. }
  73.  
  74.  
  75. function showcase_importSettings() {
  76.     var strings = document.getElementById("prefsStrings");
  77.  
  78.     var pattern = showcase_loadFromFile();
  79.   if(!pattern) return false;
  80.  
  81.   var i;
  82.   var showcaseImport = new Array;
  83.   var appendFilters = null;
  84.  
  85.   if(pattern[1].indexOf("Showcase - Settings") < 0) {
  86.       alert(strings.getString('prefs.invalid'));
  87.       return false;
  88.   }
  89.  
  90.   if(!confirm(strings.getString('prefs.import'))) return false;
  91.  
  92.   for(i = 6; i < pattern.length; i++) {
  93.       var index = pattern[i].indexOf("=");
  94.  
  95.       if(index > 0) {
  96.           showcaseImport[i] = [];
  97.       showcaseImport[i].push(pattern[i].substring(0, index));
  98.       showcaseImport[i].push(pattern[i].substring(index + 1, pattern[i].length));
  99.         }
  100.     }
  101.  
  102.     if(pattern[1].indexOf("Showcase - Settings") >= 0) {
  103.     for(i = 6; i < showcaseImport.length; i++) {
  104.             try {
  105.                 switch(showcase_gPrefBranch.getPrefType(showcaseImport[i][0])) {
  106.                     case Components.interfaces.nsIPrefBranch.PREF_BOOL:
  107.                              showcase_gPrefBranch.setBoolPref(showcaseImport[i][0],/true/i.test(showcaseImport[i][1]));
  108.                break;
  109.  
  110.           case Components.interfaces.nsIPrefBranch.PREF_INT:
  111.                showcase_gPrefBranch.setIntPref(showcaseImport[i][0], showcaseImport[i][1]);
  112.                break;
  113.  
  114.           case Components.interfaces.nsIPrefBranch.PREF_STRING:
  115.                var pref = showcaseImport[i][1];
  116.                if(pref.indexOf('"') == 0) // in prev version we use " " for string
  117.                                      pref = pref.substring(1,pref.length - 1);
  118.                showcase_gPrefBranch.setCharPref(showcaseImport[i][0], pref);
  119.                break;
  120.                 }
  121.             }
  122.             catch(e) { }
  123.         }
  124.  
  125.             window.alert(strings.getString('prefs.importsuccess'));
  126.         return true;
  127.     }
  128.  
  129.     alert(strings.getString('prefs.importfailed'));
  130.     return false;
  131. }
  132.  
  133.  
  134. function showcase_saveToFile(patterns) {
  135.   var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
  136.   var stream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
  137.  
  138.     var strings = document.getElementById("prefsStrings");
  139.  
  140.   fp.init(window, strings.getString('prefs.save'), fp.modeSave);
  141.   fp.defaultExtension = 'showcase';
  142.   fp.defaultString = strings.getString('prefs.defaultFileName') + '.' + fp.defaultExtension;
  143.   fp.appendFilter(strings.getString('prefs.filter'), "*.showcase");
  144.   fp.appendFilters(fp.filterAll);
  145.  
  146.   if(fp.show() != fp.returnCancel) {
  147.  
  148.     if(fp.file.exists()) fp.file.remove(true);
  149.     fp.file.create(fp.file.NORMAL_FILE_TYPE, 0666);
  150.     stream.init(fp.file, 0x02, 0x200, null);
  151.  
  152.     for(var i = 0; i < patterns.length; i++) {
  153.       patterns[i] = patterns[i] + "\n";
  154.       stream.write(patterns[i], patterns[i].length);
  155.     }
  156.  
  157.     stream.close();
  158.   } else {
  159.   }
  160. }
  161.  
  162.  
  163. function showcase_loadFromFile() {
  164.     var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
  165.   var stream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
  166.   var streamIO = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
  167.  
  168.     var strings = document.getElementById("prefsStrings");
  169.  
  170.   fp.init(window, strings.getString('prefs.open'), fp.modeOpen);
  171.   fp.defaultExtension = 'showcase';
  172.   fp.appendFilter(strings.getString('prefs.filter'), "*.showcase");
  173.   fp.appendFilters(fp.filterAll);
  174.  
  175.   if(fp.show() != fp.returnCancel) {
  176.  
  177.       stream.init(fp.file, 0x01, 0444, null);
  178.     streamIO.init(stream);
  179.  
  180.     var input = streamIO.read(stream.available());
  181.     streamIO.close();
  182.     stream.close();
  183.  
  184.     var linebreak = input.match(/(((\n+)|(\r+))+)/m)[1]; // first: whole match -- second: backref-1 -- etc..
  185.     return input.split(linebreak);
  186.   }
  187.  
  188.   return null;
  189. }
  190.